from decimal import Decimal
from django.shortcuts import render

# Create your views here.
from django_redis import get_redis_connection


from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView

from goods.models import SKU
from orders.models import OrderInfo
from orders.serializers import OrderSKUSerializer, OrderSerializer, OrderPlaceSerializer

"""
订单列表展示　　　
必须是登陆用户才可以访问

1.我们获取用户信息
２．从redis中获取数据
３．需要获取的是选中的数据
４．[sku_id,sku_id]
5.[SKU,SKU.SKU]
6.返回响应

GET  /orders/placeorders/
"""
class PlaceOrderAPIView(APIView):
    permission_classes = [IsAuthenticated]
    def get(self,request):
        # 1.我们获取用户信息
        user = request.user
        # ２．从redis中获取数据
        redis_conn = get_redis_connection('cart')
        # hash
        redis_id_count = redis_conn.hgetall('cart_%s'%user.id)
        # set
        selected_ids = redis_conn.smembers('cart_selected_%s'%user.id)
        # ３．需要获取的是选中的数据
        # 同时对bytes类型进行转化
        selected_cart = {}   # {sku_id:count}
        for sku_id in selected_ids:
            selected_cart[int(sku_id)] = int(redis_id_count[sku_id])

        # ４．[sku_id,sku_id]
        ids = selected_cart.keys()
        # 5.[SKU,SKU.SKU]
        skus = SKU.objects.filter(pk__in = ids)
        # 商品个数
        for sku in skus:
            sku.count = selected_cart[sku.id]
        # 6.返回响应
        # serializer = OrderSKUSerializer(skus,many=True)
        # data = {
        #     'freight':10,
        #     'skus':serializer.data
        # }
        freight = Decimal('10.00')
        serializer = OrderPlaceSerializer({
            'freight':freight,
            'skus':skus
        })
        return Response(serializer.data)

"""
提交订单

１．接受前端数据(用户信息，地址，支付方式)
２．验证数据
３．数据入库
４．返回响应
"""
from rest_framework.generics import CreateAPIView, GenericAPIView


class OrderAPIView(CreateAPIView):
    permission_classes = [IsAuthenticated]
    serializer_class = OrderSerializer


# 订单列表展示
# """
# 获取登录用户的订单列表数据并在用户中心订单列表页面进行展示。
# """
# """
# 1.必须是登录用户
# 2.获取用户信息
# ３．从redis中获取数据
# """



# 用户订单列表显示需求
from rest_framework.generics import ListAPIView
from orders.serializers import AllOrdersSerializer
from rest_framework.filters import OrderingFilter
class AllOrders(ListAPIView):
    permission_classes = [IsAuthenticated]
    queryset = OrderInfo.objects.all()
    serializer_class = AllOrdersSerializer
    filter_backends = [OrderingFilter]
    ordering_fields = ('create_time', 'total_amount',)

# class PayOrder(ListAPIView):
#     permission_classes = [IsAuthenticated]






from orders.serializers import ScoreSerializer
class ScoreAPIView(ListAPIView):
    permission_classes = [IsAuthenticated]
    serializer_class = ScoreSerializer

# 评论信息展示
from orders.serializers import CommentDetailSerializer
class CommentDetailAPIView(APIView):
    def get(self,request,sku_id):

        sku_id = OrderGoods.objects.filter(sku_id=int(sku_id))
        serializer = CommentDetailSerializer(sku_id,many=True)
        return Response(serializer.data)


# 评论界面显示
from orders.models import OrderGoods
from orders.serializers import GoodsJudgeSerializer

class GoodsJudgeAPIView(APIView):

    permission_classes = [IsAuthenticated]

    def get(self, request, order_id):

        skus = OrderGoods.objects.filter(order_id=order_id)

        serializer = GoodsJudgeSerializer(skus, many=True)

        return Response(serializer.data)



# 评论
from orders.serializers import CommentSerializer
class CommentAPIView(APIView):
    permission_classes = [IsAuthenticated]

    def post(self,request,order_id):

        data = request.data
        serializer = CommentSerializer(data=data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data)

